package edu.tgc.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import edu.tgc.service.system.SysPropertiesService;

@Aspect
public class DevelopmentModeAdvise {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    protected SysPropertiesService sysPropertiesService;

    // @Around(value = "execution(* org.springframework.security..*.vote(..)) and args(authentication,..)")
    public Object around(ProceedingJoinPoint joinPoint, Authentication authentication) throws Throwable {
        String devModeCode = this.sysPropertiesService.getDevModeCode();
        if (devModeCode != null && authentication.getAuthorities().contains(new SimpleGrantedAuthority(devModeCode))) {
            return org.springframework.security.access.AccessDecisionVoter.ACCESS_GRANTED;
        } else {
            return joinPoint.proceed();
        }
    }

}
